package com.ny.view;

import com.ny.entity.Student;
import com.ny.service.Impl.StudentServiceImpl;
import com.ny.service.StudentService;

import java.sql.SQLException;
import java.util.Scanner;

public class ViewMain {
        private static StudentService service ;
        public static void main(String[] args) throws Exception {
            service = new StudentServiceImpl();
            Student student = new Student();
            boolean flag = true;
            while (flag){
                System.out.println("欢迎来到校园商品售卖管理系统");
                System.out.println("请选择你的操作");
                System.out.println("1.用户登录");
                System.out.println("2.管理员登录");
                System.out.println("3.注册账号");
                System.out.println("0.退出系统");
                Scanner scanner  = new Scanner(System.in);
                int i = scanner.nextInt();
                switch (i){
                    case 1:
                        System.out.println("用户登录");
                        System.out.println("请输入用户名");
                        String studentName = scanner.next();
                        System.out.println("请输入密码");
                        String studentPassword = scanner.next();
                        student.setStudentName(studentName);
                        student.setStudentPassword(studentPassword);

                        student = service.login(studentName, studentPassword);
                        if(student!=null){
                            System.out.println("登录成功");
                            IndexView indexView = new IndexView();
                            indexView.indexView(student);
                        }
                        else {
                            System.out.println("登录失败");
                        }
                        break;
                    case 2:
                        System.out.println("管理员登录");
                        System.out.println("请输入管理员用户名：");
                        String adminName = scanner.next();
                        System.out.println("请输入管理员密码：");
                        String adminPassword = scanner.next();

                        // 假设管理员登录也由 StudentService 提供支持
                        Student admin = service.login(adminName, adminPassword);
                        if (admin != null ) { // 判断是否为管理员
                            System.out.println("管理员登录成功");
                            ManagerView  managerView = new ManagerView();
                            managerView.showManagerView();
                        } else {
                            System.out.println("管理员登录失败");
                        }
                        break;
                    case 3:
                        System.out.println("请输入用户名");
                        student.setStudentName(scanner.next());
                        System.out.println("请输入密码");
                        student.setStudentPassword(scanner.next());
                        System.out.println("请输入手机号");
                        student.setStudentPhone(scanner.next());
                        int add = service.add(student);
                        if(add>0){
                            System.out.println("注册成功");
                        }
                        else {
                            System.out.println("注册失败");
                            System.out.println("请重新输入");
                            ViewMain.main(args);
                        }
                        break;
                    case 4:
                        flag = false;
                        break;
                }
            }
        }
    }

